package com.fas.system.service.impl;

import com.fas.common.core.redis.RedisCache;
import com.fas.system.domain.FasMiniatureStation;
import com.fas.system.domain.SysUserMiniatureStation;
import com.fas.system.mapper.FasMiniatureStationMapper;
import com.fas.system.service.IFasMiniatureStationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class FasMiniatureStationServiceImpl implements IFasMiniatureStationService {

    @Resource
    private FasMiniatureStationMapper fasMiniatureStationMapper;
    @Resource
    private RedisCache redisCache;

    @Override
    public List<FasMiniatureStation> findAllEquipment(FasMiniatureStation fasMiniatureStation) {
        List<FasMiniatureStation> list = fasMiniatureStationMapper.findAllEquipment(fasMiniatureStation);
        list.forEach(ll -> ll.setCompanyName(FasCooperationCompanyServiceImpl.COMPANY_NAMES.get(ll.getCooperationCompanyId())));
        return list;
    }

    @Override
    public List<FasMiniatureStation> findLikeEngineer(FasMiniatureStation fasMiniatureStation) {
        List<FasMiniatureStation> list = fasMiniatureStationMapper.findLikeEngineer(fasMiniatureStation);
        list.forEach(ll -> ll.setCompanyName(FasCooperationCompanyServiceImpl.COMPANY_NAMES.get(ll.getCooperationCompanyId())));
        return list;
    }

    @Override
    public int insertFasMiniatureStation(FasMiniatureStation fasMiniatureStation) {
        return fasMiniatureStationMapper.insertFasMiniatureStation(fasMiniatureStation);
    }

    @Override
    public int updateFasMiniatureStation(FasMiniatureStation fasMiniatureStation) {
        return fasMiniatureStationMapper.updateFasMiniatureStation(fasMiniatureStation);
    }

    @Override
    public int deleteFasMiniatureStation(Integer[] ids) {
        return fasMiniatureStationMapper.deleteFasMiniatureStation(ids);
    }

    @Override
    public FasMiniatureStation findById(Integer id, Long deptId) {
        FasMiniatureStation fasMiniatureStation = fasMiniatureStationMapper.findById(id);
        List<Long> userIdList = fasMiniatureStation.getUserIdList();

        List<SysUserMiniatureStation> sysUsersList = fasMiniatureStationMapper.selectUserByDeptId(deptId);

        if(sysUsersList != null && sysUsersList.size()>0){
            for (SysUserMiniatureStation sysUser : sysUsersList) {
                Long userId = sysUser.getUserId();
                if (userIdList.contains(userId)){
                    sysUser.setVerifyFlag(1);
                }
            }
        }
        fasMiniatureStation.setUsers(sysUsersList);
        return fasMiniatureStation;
    }
}
